﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Services;
using Common;
using EntityModel;
using System.Data.Entity;
using MvcPaging;

namespace Heart.Controllers
{
    public class ProductController : Controller
    {
        #region 公用属性
        private CategoryService categoryService = new CategoryService();
        private BrandService brandService = new BrandService();
        private MethodService methodService = new MethodService();
        private ProductService productService = new ProductService();
        private int pageSize = ConfigHelper.PageSize();
        #endregion

        public ActionResult Index(int? page)
        {
            int PageIndex = page.HasValue ? (page.Value == 0 ? 0 : (page.Value - 1)) : 0;
            var list = productService.GetAllList().ToPagedList(PageIndex, pageSize);
            return View(list);
        }
        public ActionResult Create()
        {
            IEnumerable<Category> category = categoryService.GetList();
            ViewData["category"] = new SelectList(category, "Id", "Name");
            IEnumerable<Method> method = methodService.GetAllList();
            ViewData["method"] = new SelectList(method, "Id", "Name");
            IEnumerable<Brand> brand = brandService.GetList();
            ViewData["brand"] = new SelectList(brand, "Id", "Name");
            return View();
        }
        [ValidateInput(false)]
        [ActionName("Create"), AcceptVerbsAttribute("POST")]
        public ActionResult Create(FormCollection c)
        {
            this.ValidateRequest = false;
            Product p = new Product();
            p.Name = WebBase.GetForm("Name");
            //热销商品
            bool? isHot = string.IsNullOrEmpty(c["IsHot"]) ? true : c["IsHot"].ToLower() != "false";
            p.ShowPlace = "";
            if (isHot ?? false)
            {
                p.ShowPlace += "isHot|";
            }
            //新品上架
            bool? isNew = string.IsNullOrEmpty(c["IsNew"]) ? true : c["IsNew"].ToLower() != "false";
            if (isNew ?? false)
            {
                p.ShowPlace += "isNew|";
            }
            //特价产品
            bool? isPrice = string.IsNullOrEmpty(c["IsPrice"]) ? true : c["IsPrice"].ToLower() != "false";
            if (isPrice ?? false)
            {
                p.ShowPlace += "isPrice|";
            }
            //店长推荐
            bool? isRecommend = string.IsNullOrEmpty(c["IsRecommend"]) ? true : c["IsRecommend"].ToLower() != "false";
            if (isRecommend ?? false)
            {
                p.ShowPlace += "isRecommend";
            }
            //是否包邮
            bool? isPost = string.IsNullOrEmpty(c["isPost"]) ? true : c["isPost"].ToLower() != "false";
            if (isRecommend ?? false)
            {
                p.IsPost = true;
            }
            p.CategoryId = WebBase.GetFormInt("CategoryId");
            p.MethodId = WebBase.GetFormInt("MethodId");
            p.BrandId = WebBase.GetFormInt("BrandId");
            p.CostPrice = WebBase.GetFormDecimal("CostPrice");
            p.CreateOn = DateTime.Now;
            p.SalePrice = WebBase.GetFormDecimal("SalePrice");
            p.Sku = WebBase.GetForm("SKU");
            p.Status = 1;
            p.BeginTime = WebBase.GetFormDateTime("BeginTime");
            p.ShortUrl = WebBase.GetForm("ShortUrl");
            p.Quantity = WebBase.GetFormInt("Quantity");
            p.MetaKeywords = WebBase.GetForm("MetaKeywords");
            p.MetaDescription = WebBase.GetForm("MetaDescription");
            p.Contents = WebBase.GetForm("Contents");
            int pid = productService.Create(p);
            return RedirectToAction("Create", "ProductAlbum", new { pid = pid });

        }

        public ActionResult Update()
        {
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public void Update(FormCollection f)
        {
            string contents = WebBase.GetForm("Contents");
            productService.EditContents(contents);

        }

        public ActionResult Edit(int pid)
        {
            IEnumerable<Category> category = categoryService.GetList();
            ViewData["category"] = new SelectList(category, "Id", "Name");
            IEnumerable<Method> method = methodService.GetAllList();
            ViewData["method"] = new SelectList(method, "Id", "Name");
            IEnumerable<Brand> brand = brandService.GetList();
            ViewData["brand"] = new SelectList(brand, "Id", "Name");
            return View(productService.GetModel(pid));
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(FormCollection c)
        {

            this.ValidateRequest = false;
            Product p = new Product();
            p.Name = WebBase.GetForm("Name");
            //热销商品
            bool? isHot = string.IsNullOrEmpty(c["IsHot"]) ? true : c["IsHot"].ToLower() != "false";
            p.ShowPlace = "";
            if (isHot ?? false)
            {
                p.ShowPlace += "isHot|";
            }
            //新品上架
            bool? isNew = string.IsNullOrEmpty(c["IsNew"]) ? true : c["IsNew"].ToLower() != "false";
            if (isNew ?? false)
            {
                p.ShowPlace += "isNew|";
            }
            //特价产品
            bool? isPrice = string.IsNullOrEmpty(c["IsPrice"]) ? true : c["IsPrice"].ToLower() != "false";
            if (isPrice ?? false)
            {
                p.ShowPlace += "isPrice|";
            }
            //店长推荐
            bool? isRecommend = string.IsNullOrEmpty(c["IsRecommend"]) ? true : c["IsRecommend"].ToLower() != "false";
            if (isRecommend ?? false)
            {
                p.ShowPlace += "isRecommend";
            }
            //是否包邮
            bool? isPost = string.IsNullOrEmpty(c["isPost"]) ? true : c["isPost"].ToLower() != "false";
            if (isRecommend ?? false)
            {
                p.IsPost = true;
            }
            p.CategoryId = WebBase.GetFormInt("CategoryId");
            p.MethodId = WebBase.GetFormInt("MethodId");
            p.BrandId = WebBase.GetFormInt("BrandId");
            p.CostPrice = WebBase.GetFormDecimal("CostPrice");
            p.CreateOn = DateTime.Now;
            p.SalePrice = WebBase.GetFormDecimal("SalePrice");
            p.Sku = WebBase.GetForm("SKU");
            p.Status = 1;
            p.BeginTime = WebBase.GetFormDateTime("BeginTime");
            p.ShortUrl = WebBase.GetForm("ShortUrl");
            p.Quantity = WebBase.GetFormInt("Quantity");
            p.MetaKeywords = WebBase.GetForm("MetaKeywords");
            p.MetaDescription = WebBase.GetForm("MetaDescription");
            p.Contents = WebBase.GetForm("Contents");
            p.Id = WebBase.GetFormInt("Id");
            productService.Edit(p);
            return RedirectToAction("Index");
        }

    }
}
